package com.pengheng.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.pengheng.config.AlipayTemplate;
import com.pengheng.vo.PayVo;
import com.pengheng.vo.QueryRefoundVo;
import com.pengheng.vo.RefoundVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @remark
 * @Author pengheng
 * @date 2021/5/19 10:02
 */
@RestController
public class AliPayController {

    @Resource
    private AlipayTemplate alipayTemplate;

    /**
     * 支付订单
     * @param orderNo
     * @return
     * @throws AlipayApiException
     */
    @GetMapping(value = "/{orderNo}", produces = "text/html")
    public String hello(@PathVariable("orderNo") String orderNo) throws AlipayApiException {
        PayVo payVo = new PayVo();
        payVo.setBody("I am body");
        payVo.setSubject("I am subject");
        payVo.setTotal_amount("12000.00");
        payVo.setOut_trade_no(orderNo);
        return alipayTemplate.pay(payVo);
    }

    @GetMapping(value = "/success")
    public String success() throws AlipayApiException {
        return "支付成功";
    }

    @GetMapping(value = "/notify")
    public String notifyMethod() {
        return "支付成功";
    }

    /**
     * 查询交易记录
     * @param outTradeNo
     * @param tradeNo
     * @return
     * @throws AlipayApiException
     */
    @GetMapping(value = "/query/{outTradeNo}/{tradeNo}")
    public String queryPay(@PathVariable("outTradeNo") String outTradeNo, @PathVariable("tradeNo") String tradeNo) throws AlipayApiException {
        return alipayTemplate.queryPay(outTradeNo, tradeNo);
    }

    /**
     * 退款
     * @param outTradeNo
     * @param tradeNo
     * @return
     * @throws AlipayApiException
     */
    @GetMapping(value = "/refound/{outTradeNo}/{tradeNo}")
    public String refound(@PathVariable("outTradeNo") String outTradeNo, @PathVariable("tradeNo") String tradeNo) throws AlipayApiException {
        RefoundVo refoundVo = new RefoundVo();
        refoundVo.setOut_trade_no(outTradeNo);
        refoundVo.setTrade_no(tradeNo);
        refoundVo.setRefund_reason("不想要了");
        refoundVo.setRefund_amount("12000.00");
        return alipayTemplate.refound(refoundVo);
    }

    /**
     * 查询退款
     * @param outTradeNo
     * @param tradeNo
     * @return
     * @throws AlipayApiException
     */
    @GetMapping(value = "/queryRefound/{outTradeNo}/{tradeNo}")
    public String queryRefound(@PathVariable("outTradeNo") String outTradeNo, @PathVariable("tradeNo") String tradeNo) throws AlipayApiException {
        QueryRefoundVo queryRefoundVo = new QueryRefoundVo();
        queryRefoundVo.setOut_trade_no(outTradeNo);
        queryRefoundVo.setTrade_no(tradeNo);
        return alipayTemplate.queryRefound(queryRefoundVo);
    }


    /**
     * 关闭订单
     * @param outTradeNo
     * @param tradeNo
     * @return
     * @throws AlipayApiException
     */
    @GetMapping(value = "/close/{outTradeNo}/{tradeNo}")
    public String close(@PathVariable("outTradeNo") String outTradeNo, @PathVariable("tradeNo") String tradeNo) throws AlipayApiException {
        return alipayTemplate.closeOrder(outTradeNo,tradeNo);
    }
}
